package cn.catsdad.service.impl;

import cn.catsdad.base.service.impl.BaseServiceImpl;
import cn.catsdad.dto.RoleMenuDTO;
import cn.catsdad.repository.RoleMenuRepository;
import cn.catsdad.service.api.RoleMenuService;
import cn.catsdad.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 权限与菜单关联service实现
 *
 * @author xuyonglong
 * @version V1.0 2023/4/13
 */
@Service
public class RoleMenuServiceImpl extends BaseServiceImpl<RoleMenuDTO, Long, RoleMenuRepository>
        implements RoleMenuService {

    /**
     * 查询角色具备的权限id
     *
     * @param roleIds 角色id
     * @return 菜单id
     */
    @Override
    public Set<Long> queryMenuIdsByRoles(Collection<Long> roleIds) {
        Set<Long> result = new HashSet<>();
        if (StringUtil.isNotEmpty(roleIds)) {
            List<RoleMenuDTO> roleMenus = repository.findByRoleIdIn(roleIds);
            result = roleMenus.stream().map(RoleMenuDTO::getMenuId).collect(Collectors.toSet());
        }
        return result;
    }

    @Override
    @Transactional
    public void deleteByRoleIds(Collection<Long> roleIds) {
        if (StringUtil.isNotEmpty(roleIds)) {
            repository.deleteByRoleIdIn(roleIds);
        }
    }

}
